package com.ldk.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ldk.base.framework.util.DateUtil;
import com.ldk.base.framework.util.HttpUtil;
import com.ldk.framework.entity.Company;
import com.ldk.framework.entity.Companyfare;
import com.ldk.framework.entity.Companypay;
import com.ldk.framework.entity.Companypermit;
import com.ldk.framework.entity.Companyservice;
import com.ldk.framework.entity.Companystat;
import com.ldk.framework.entity.Driverapp;
import com.ldk.framework.entity.Drivereducate;
import com.ldk.framework.entity.Driverstat;
import com.ldk.framework.entity.OperateArrive;
import com.ldk.framework.entity.OperateDepart;
import com.ldk.framework.entity.OperateLogin;
import com.ldk.framework.entity.OperateLogout;
import com.ldk.framework.entity.OperatePay;
import com.ldk.framework.entity.OrderCancel;
import com.ldk.framework.entity.OrderCreate;
import com.ldk.framework.entity.OrderMatch;
import com.ldk.framework.entity.Passenger;
import com.ldk.framework.entity.PlatformDriver;
import com.ldk.framework.entity.PositionDriver;
import com.ldk.framework.entity.PositionVehicle;
import com.ldk.framework.entity.RatedDriver;
import com.ldk.framework.entity.RatedDriverpunish;
import com.ldk.framework.entity.RatedPassenger;
import com.ldk.framework.entity.RatedPassengercomplaint;
import com.ldk.framework.entity.Vehicle;
import com.ldk.framework.entity.Vehicleinsurance;
import com.ldk.framework.entity.Vehicletotalmile;
import com.ldk.framework.service.ICompanyService;
import com.ldk.framework.service.ICompanyfareService;
import com.ldk.framework.service.ICompanypayService;
import com.ldk.framework.service.ICompanypermitService;
import com.ldk.framework.service.ICompanyserviceService;
import com.ldk.framework.service.ICompanystatService;
import com.ldk.framework.service.IDriverappService;
import com.ldk.framework.service.IDrivereducateService;
import com.ldk.framework.service.IDriverstatService;
import com.ldk.framework.service.IOperateArriveService;
import com.ldk.framework.service.IOperateDepartService;
import com.ldk.framework.service.IOperateLoginService;
import com.ldk.framework.service.IOperateLogoutService;
import com.ldk.framework.service.IOperatePayService;
import com.ldk.framework.service.IOrderCancelService;
import com.ldk.framework.service.IOrderCreateService;
import com.ldk.framework.service.IOrderMatchService;
import com.ldk.framework.service.IPassengerService;
import com.ldk.framework.service.IPlatformDriverService;
import com.ldk.framework.service.IPositionDriverService;
import com.ldk.framework.service.IPositionVehicleService;
import com.ldk.framework.service.IRatedDriverService;
import com.ldk.framework.service.IRatedDriverpunishService;
import com.ldk.framework.service.IRatedPassengerService;
import com.ldk.framework.service.IRatedPassengercomplaintService;
import com.ldk.framework.service.IUserService;
import com.ldk.framework.service.IVehicleService;
import com.ldk.framework.service.IVehicleinsuranceService;
import com.ldk.framework.service.IVehicletotalmileService;

import org.apache.commons.codec.binary.Hex;
import org.apache.http.util.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 支付通知控制器
 *
 * @author mqy
 */
@Controller
@RequestMapping("/reportdatagy")
public class ReportDataGYController extends BaseController {

    @Autowired
    private ICompanypermitService companypermitService;
    @Autowired
    private ICompanyService companyService;
    @Autowired
    private ICompanystatService companystatService;
    @Autowired
    private ICompanypayService companypayService;
    @Autowired
    private ICompanyfareService companyfareService;
    @Autowired
    private ICompanyserviceService companyserviceService;
    @Autowired
    private IVehicleService vehicleService;
    @Autowired
    private IVehicleinsuranceService vehicleinsuranceService;
    @Autowired
    private IVehicletotalmileService vehicletotalmileService;
    @Autowired
    private IPlatformDriverService platformDriverService;
    @Autowired
    private IDrivereducateService drivereducateService;
    @Autowired
    private IDriverappService driverappService;
    @Autowired
    private IDriverstatService driverstatService;
    @Autowired
    private IPassengerService passengerService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IOrderCreateService orderCreateService;
    @Autowired
    private IOrderCancelService orderCancelService;
    @Autowired
    private IOrderMatchService orderMatchService;
    @Autowired
    private IOperatePayService operatePayService;

    @Autowired
    private IOperateLoginService operateLoginService;
    @Autowired
    private IOperateLogoutService operateLogoutService;
    @Autowired
    private IOperateDepartService operateDepartService;
    @Autowired
    private IOperateArriveService operateArriveService;

    @Autowired
    private IPositionDriverService positionDriverService;
    @Autowired
    private IPositionVehicleService positionVehicleService;
    @Autowired
    private IRatedPassengerService ratedPassengerService;
    @Autowired
    private IRatedDriverpunishService ratedDriverpunishService;
    @Autowired
    private IRatedPassengercomplaintService ratedPassengercomplaintService;
    @Autowired
    private IRatedDriverService ratedDriverService;

    private String posturl = "http://222.85.224.5:9080/gjza/imp/net/";
    private String authtoken = "ed03a2cd3d67420b9111c8c1a8710bd3";

    /**
     * 上传数据至贵阳市公安公交云系统管理
     *
     * @param method
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/postTransitData", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE)
    public String postTransitData(@RequestParam(value = "method", required = false, defaultValue = "1") String method) throws UnsupportedEncodingException {

        List<Vehicle> list = vehicleService.selectAll(null);
        List<PlatformDriver> driverlist = platformDriverService.selectAll(null);
        for (int i = 0; i < list.size(); i++) {
            JSONArray jsonArray = new JSONArray();
            Vehicle companypermit = list.get(i);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("plateNumber", companypermit.getVehicleNo());
            jsonObject.put("carType", 0);
            jsonObject.put("truckColor", companypermit.getVehicleColor());
            jsonObject.put("brand", companypermit.getBrand());
            jsonObject.put("plateModel", companypermit.getModel());
            jsonObject.put("fuelType", "汽油");
            jsonObject.put("isDel", "0");

            JSONArray driverArray = new JSONArray();
            JSONObject jsonObject1 = getDriverInfo(driverlist, companypermit.getDriverPhone());
            if (jsonObject1 == null || TextUtils.isEmpty(jsonObject1.getString("phone"))) {
                continue;
            }
            driverArray.add(jsonObject1);

            JSONArray equipsArray = new JSONArray();
            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("equipName", "巡逻鹰GPS");
            jsonObject2.put("equipStatus", "正常");
            jsonObject2.put("installTime", companypermit.getCertifyDateA());
            jsonObject2.put("isDel", "0");
            equipsArray.add(jsonObject2);

            jsonObject.put("drivers", driverArray);
            jsonObject.put("equips", equipsArray);

            jsonArray.add(jsonObject);

            String json = JSON.toJSONString(jsonArray);

            Map<String, String> headers = new HashMap<String, String>();
            headers.put("X-Authorization", authtoken);
            headers.put("Content-Type", "application/json");

            String decode = URLEncoder.encode(json, "UTF-8");
            String url = posturl + "postTransitData?transits=" + decode;
            //System.out.print("url=" + url);
            String response = HttpUtil.post(url, null, headers);
            System.out.println("response=" + response);
            if(!response.contains("1000")){
                System.out.print("json=" + json);
            }
        }

        /*String json = JSON.toJSONString(jsonArray);
        //System.out.println("json=" + json);
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("X-Authorization", authtoken);
        headers.put("Content-Type", "application/json");

        String decode = URLEncoder.encode(json, "UTF-8");
        String url = posturl + "postTransitData?transits=" + decode;
        System.out.print("url=" + url);

        String response = HttpUtil.post(url, null, headers);
        System.out.print("response=" + response);*/
        return "成功";
    }

    private JSONObject getDriverInfo(List<PlatformDriver> driverList, String phoneNo) {
        JSONObject jsonObject = new JSONObject();
        for (PlatformDriver driver : driverList) {
            if (driver.getDriverPhone()!=null&&phoneNo!=null&&driver.getDriverPhone().equals(phoneNo)) {
                jsonObject.put("realName", driver.getDriverName());
                jsonObject.put("identityId", driver.getLicenseId());
                jsonObject.put("phone", driver.getDriverPhone());
                jsonObject.put("sex", driver.getDriverGender());
                jsonObject.put("dayOfBirth", driver.getDriverBirthday());
                jsonObject.put("placeOfOrigin", "贵州" + driver.getContractCompany());
                jsonObject.put("homePlace", driver.getDriverContactAddress());
                jsonObject.put("hasLocal", "1");
                jsonObject.put("isDel", "0");
            }
        }
        return jsonObject;
    }


    /**
     * 上传数据至贵阳市公安公交
     *
     * @param method
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/postGpsData", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE)
    public String postGpsData(@RequestParam(value = "method", required = false, defaultValue = "1") String method) throws UnsupportedEncodingException {

        List<Vehicle> list = vehicleService.selectAll(null);
        List<PositionVehicle> positionlist = positionVehicleService.selectAll(null);
        for (int i = 0; i < list.size(); i++) {
            JSONArray jsonArray = new JSONArray();
            Vehicle vehicle = list.get(i);
            if (TextUtils.isEmpty(vehicle.getDriverPhone())) {
                continue;
            }
            PositionVehicle positionVehicle = getPositionVehicle(positionlist, vehicle.getVehicleNo());
            if (positionVehicle == null) {
                continue;
            }
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("plateNumber", positionVehicle.getVehicleNo());
            jsonObject.put("gpsSpeed", 20.6);
            jsonObject.put("gpsTime", positionVehicle.getPositionTime());
            jsonObject.put("isRun", "1");
            jsonObject.put("gpsLng", positionVehicle.getLongitude());
            jsonObject.put("gpsLat", positionVehicle.getLatitude());
            jsonArray.add(jsonObject);

            String json = JSON.toJSONString(jsonArray);
            //System.out.println("json=" + json);
            Map<String, String> headers = new HashMap<String, String>();
            headers.put("X-Authorization", authtoken);
            headers.put("Content—Type", "application/json");
            String decode = URLEncoder.encode(json, "UTF-8");
            String url = posturl + "postGpsData?gps=" + decode;
            //System.out.print("url=" + url);
            String response = HttpUtil.post(url, null, headers);
            System.out.println("response=" + response);
            if(!response.contains("1000")){
                System.out.print("json=" + json);
            }
        }

        /*String json = JSON.toJSONString(jsonArray);
        System.out.println("json=" + json);
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("X-Authorization", authtoken);
        headers.put("Content—Type", "application/json");
        String decode = URLEncoder.encode(json, "UTF-8");
        String url = posturl + "postGpsData?gps=" + decode;
        //System.out.print("url=" + url);
        String response = HttpUtil.post(url, null, headers);
        System.out.print("response=" + response);*/
        return "成功";
    }

    private Long tranDouble(Double value) {
        if (value == null) {
            return 0L;
        }
        long v = Long.parseLong(value.toString().replace(".", ""));
        return v;
    }

    private PositionVehicle getPositionVehicle(List<PositionVehicle> driverList, String vehicleNo) {
        for (PositionVehicle driver : driverList) {
            if (driver.getVehicleNo().equals(vehicleNo)) {
                return driver;
            }
        }
        return null;
    }
}
